<?PHP
	session_start();
	session_cache_expire(30);
?>
<!--
	calendar.inc
	@author Max Palmer and Allen Tucker
	@version 3/26/08, revised 9/15/2008
-->

<?php
	/**
	* shows the "previous week"/"next week" menu, if the weeks exist
	*/
	function do_week_nav($week, $edit, $loc) {
		$cur_id=$week->get_id();
		$prev_id=date("m-d-y",mktime(0,0,0,substr($cur_id,0,2),substr($cur_id,3,2)-7,substr($cur_id,6,2)));
		$next_id=date("m-d-y",mktime(0,0,0,substr($cur_id,0,2),substr($cur_id,3,2)+7,substr($cur_id,6,2)));
		$s="<p align=\"center\"><table id=\"weeknav\" align=\"center\"><tr><td align=\"left\">";
		// determines if there is a previous week
		$prev_week=get_dbWeeks($prev_id);
		if($prev_week instanceof Week) {
			$s=$s.'<a href="calendar'.$loc.'.php?id='.$prev_id;
			if($edit)
				$s=$s."&edit=true";
			$s=$s."\" id=\"weeknavlink\"><< Previous Week</a>";
		}
		$s=$s."</td>";

		$s = $s . "<td align=\"right\">";
		// determines if there is a next week
		$next_week=get_dbWeeks($next_id);
		if($next_week instanceof Week) {
			$s=$s.'<a href="calendar'.$loc.'.php?id='.$next_id;
			if($edit)
				$s=$s."&edit=true";
			$s=$s."\" id=\"weeknavlink\">Next Week >></a>";
		}

		$s=$s."</td></tr><tr>" ;
		if($_SESSION['access_level']>=1 )
		  if ($edit != true)
		    $s = $s . '<td align="left"> <a href="calendar' . $loc . '.php?id=' . $cur_id . '&edit=true">(edit this week)</a></td>';
		  else
		    $s = $s . '<td align="left"> <a href="calendar' . $loc . '.php?id=' . $cur_id . '&edit=false">(view this week)</a></td>';
		if($_SESSION['access_level']>=2)
		    $s = $s . '<td align="right"> <a href="addWeek.php">(manage weeks)</a></td>';
		$s = $s."</tr></table></p>";
		return $s;
	}


	// return a string of html that contains a table cell for a shift
	function do_shift($day,$shift,$length,$edit) {
		// chooses size annd style of cell based on length
		$s= "<td colspan=\"2\" ";
		if($length==3)
			$s=$s."rowspan=\"3\""." class=\"shift"."3";
		else if($length==4)
			$s=$s."rowspan=\"4\""." class=\"shift"."4";
		else
			$s=$s."rowspan=\"1\""." class=\"shift"."1";
		// checks that the shift is not in the past
		$year=date("Y",time());
		$doy=date("z",time())+1;
		if($edit && ($year < $day->get_year() || $year == $day->get_year() && $doy <= $day->get_day_of_year() ) )
			$s=$s."e";
		$s=$s."\"><table id=\"shiftbox\"><tr><td class=\"persons\">";
		if($edit && ($year < $day->get_year() || $year == $day->get_year() && $doy <= $day->get_day_of_year() ) )
			$s=$s."<a id=\"shiftlink\" href=\"editShift.php?shift=".$day->get_id()."-".$shift."\">";
		$s=$s.get_people_for_shift($day,$shift);
		if($edit && ($year < $day->get_year() || $year == $day->get_year() && $doy <= $day->get_day_of_year() ) ) {
			$s=$s."</a>";
			// if manager, adds shift notes
			if($_SESSION['access_level']>=2) {
				$s=$s."</td></tr><tr><td class=\"notes\" align=\"center\">".
				"<textarea id=\"shift_notes\" rows=\"1\" name=\"shift_notes_".$day->get_shift($shift)->get_id()."\">"
					.$day->get_shift($shift)->get_notes()."</textarea>";
			}
			else {
				$shiftnote=$day->get_shift($shift)->get_notes();
			    $s=$s."</td></tr><tr><td class=\"notes\"><p id=\"shift\">".$shiftnote."</p>";
			}
		}
		else {
			$shiftnote=$day->get_shift($shift)->get_notes();
			$s=$s."</td></tr><tr><td class=\"notes\"><p id=\"shift\">".$shiftnote."</p>";
		}
		$s=$s."</td></tr></table></td>";
		return $s;
	}

	function get_dom($day) {
		$dom=$day->get_dom();
		if(substr($dom,0,1)=="0")
			return "&nbsp;&nbsp;".substr($dom,1);
		return $dom;
	}

	function get_people_for_shift($day,$key) {
		$shift=$day->get_shift($key);
		$people=$shift->get_persons();
		if(!$people[0])
			array_shift($people);
		$p="<br>";
		$vac=0;
		//echo $shift->get_id()." slots = ".$shift->num_slots()."  vac = ".$shift->num_vacancies()."<br>";
		for($i=0;$i<$shift->num_slots();++$i) {
			if(!$people[$i]=="") {
				$name_components=explode("+",$people[$i]);
				$p=$p."&nbsp;".$name_components[1]." ".$name_components[2]."<br>";
			}
			else
				++$vac;
		}
		if($vac>0)
			$p=$p."&nbsp;<b>Vacancies (".$vac.")</b>";
		return substr($p,0,strlen($p)-4) ;
	}

	function do_day_notes($days,$edit, $year, $doy) {
		if($edit==false || $_SESSION['access_level']<2) {
			$s="<tr><td class=\"hour\">manager notes&nbsp;</td>";
			for($i=0;$i<7;++$i) {
				$s=$s."<td class=\"note\" colspan=\"2\">".$days[$i]->get_mgr_notes()."</td>";
			}
			$s=$s."</tr><tr><td class=\"hour\">guest chef&nbsp;</td>";
			for($i=0;$i<7;++$i) {
				$s=$s."<td class=\"note\" colspan=\"2\">".$days[$i]->get_chef_notes()."</td>";
			}
			return $s."</tr>";
		}
		else {
			$s="<tr><td class=\"hour\">manager notes&nbsp;</td>";
			for($i=0;$i<7;++$i) {
				if ($year < $days[$i]->get_year() || $year == $days[$i]->get_year() && $doy <= $days[$i]->get_day_of_year())
				{
					 $s=$s."<td class=\"note_e\" colspan=\"2\"><textarea id=\"mgr_notes\"
					 rows=\"1\" name=\"mgr_notes".$i."\">"
					 .$days[$i]->get_mgr_notes()."</textarea></td>";
				}
				else
				    $s=$s."<td class=\"note\" colspan=\"2\">".$days[$i]->get_mgr_notes()."</td>";
			}

			$s=$s."<tr><td rows=\"1\" class=\"hour\">guest chef</td>";
			for($i=0;$i<7;++$i) {
				if($year < $days[$i]->get_year() || $year == $days[$i]->get_year() && $doy <= $days[$i]->get_day_of_year()) {
					$s=$s."<td class=\"note_e\" colspan=\"2\"><textarea id=\"chef_notes\" rows=\"1\" name=\"chef_notes".$i."\">"
						.$days[$i]->get_chef_notes()."</textarea></td>";
				}
				else
					$s=$s."<td class=\"note\" colspan=\"2\">".$days[$i]->get_chef_notes()."</td>";
			}
			return $s;
		}
	}

	function process_edit_notes($week,$post) {
		$days=$week->get_dates();
		$fam = "";
		foreach ($post as $key=>$value)
		   if (strpos($key, "Fam") !== false)
		      $fam = "Fam";
		if ($fam == "") {
		  for($i=0;$i<7;++$i) {
			$shifts=famfilter($days[$i]->get_shifts(),$fam);
			foreach ($shifts as $key => $shift) {
				$note=trim(str_replace('\"','\\\"',str_replace('\'','\\\'',htmlentities($post['shift_notes_'.$shift->get_id()]))));
				$shift->set_notes($note);
				update_dbShifts($shift);
				if ($shift->has_sub_call_list())
				   add_log_entry('<a href=\"viewPerson.php?id='.$_SESSION['_id'].'\">'.$_SESSION['f_name'].' '.
				       $_SESSION['l_name'].'</a> updated the <a href=\"subCallList.php?shift='.$shift->get_id().'\">sub call list</a> and the shift: <a href=\"editShift.php?shift='.
				       $shift->get_id().'\">'.$shift->get_shift_name_from_id($shift->get_id()).'</a>.');
			}
			$chef_note = trim(str_replace('\"','\\\"',str_replace('\'','\\\'',htmlentities($post['chef_notes'.$i]))));
			$days[$i]->set_chef_notes($chef_note);
			$mgr_note = trim(str_replace('\"','\\\"',str_replace('\'','\\\'',htmlentities($post['mgr_notes'.$i]))));
			$days[$i]->set_mgr_notes($mgr_note);
			update_dbDates($days[$i]);
		  }
		}
		else
		  for ($i=0;$i<5;++$i) {
		  	$shifts=famfilter($days[$i]->get_shifts(),$fam);
			foreach ($shifts as $key => $shift) {
				$note=trim(str_replace('\"','\\\"',str_replace('\'','\\\'',htmlentities($post['shift_notes_'.$shift->get_id()]))));
				$shift->set_notes($note);
				update_dbShifts($shift);
			}
		  }
	}

	// return only those shifts whose id's have $filter == "Fam" or $filter == "" as a suffix
	function famfilter($shifts, $filter){
		$fewshifts = array();
		foreach($shifts as $shift) {
		  if ($filter === "Fam") {
			if (strpos($shift->get_id(),"Fam") !== false)
			   $fewshifts[] = $shift;
		  }
	      else
	        if (strpos($shift->get_id(),"Fam") === false)
	           $fewshifts[] = $shift;
		}
        return $fewshifts;
	}
?>